package com.txsj.scan.widget.popup;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow;

import com.txsj.scan.R;


/**
 * Created by wwb on 2019/6/10.
 */

public class PopupAllocationExamine {

    Context mContext;
    PopupWindow mPopup;
    Button btn_batch, btn_epc;//
    Button btn_cancel;//取消
    LinearLayout lin_popup_root;
    View rootView;

    public PopupAllocationExamine(Context context, View view) {
        this.mContext = context;
        this.rootView = view;
    }

    public PopupAllocationExamine builder() {
        LayoutInflater layoutInflater = LayoutInflater.from(mContext);
        View v = layoutInflater.inflate(R.layout.popup_allocation_examine, null);
        btn_batch = (Button) v.findViewById(R.id.btn_batch);
        btn_epc = (Button) v.findViewById(R.id.btn_epc);
        btn_cancel = (Button) v.findViewById(R.id.btn_cancel);
        lin_popup_root = (LinearLayout) v.findViewById(R.id.lin_popup_root);
        // 为自定义的对话框设置自定义布局
        mPopup = new PopupWindow(v, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        mPopup.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
        mPopup.setFocusable(true);
        mPopup.setBackgroundDrawable(new BitmapDrawable());
        // 设置允许在外点击消失
        mPopup.setOutsideTouchable(true);
        mPopup.setAnimationStyle(R.style.anim_popu);
        btn_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPopup.dismiss();
            }
        });
        lin_popup_root.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPopup.dismiss();
            }
        });
        return this;
    }


    /**
     * 打开相机操作
     *
     * @return
     */
    public PopupAllocationExamine setFirseClick(final View.OnClickListener listener) {
        btn_batch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPopup.dismiss();
                listener.onClick(v);
            }
        });
        return this;
    }


    /**
     * 打开相册操作
     *
     * @return
     */
    public PopupAllocationExamine setSecondClick(final View.OnClickListener listener) {
        btn_epc.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mPopup.dismiss();
                listener.onClick(v);
            }
        });
        return this;
    }


    /**
     * 显示PopupWindow
     *
     * @return
     */
    public PopupAllocationExamine show() {
        mPopup.showAtLocation(rootView, Gravity.BOTTOM, 0, 0);
        return this;
    }


}
